package uni.edu.pe.analisisSentimental.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Repository;

import uni.edu.pe.analisisSentimental.dao.UserMentionDAO;
import uni.edu.pe.analisisSentimental.domain.UserMention;

@Repository
public class UserMentionDAOImpl implements UserMentionDAO {

    private static Logger logger = LoggerFactory.getLogger(UserMentionDAOImpl.class);
	private static final boolean isDebugEnabled = logger.isDebugEnabled();

	// SimpleJdbcTemplate-style...
	private SimpleJdbcTemplate simpleJdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
	    this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}
	
	/*
	 * (non-Javadoc)
	 * @see uni.edu.pe.analisisSentimental.dao.UserMentionDAO#saveAll(java.util.List)
	 */
	public void saveAll(List<UserMention> lum){
		logger.debug("<==== Inicio saveAll ====>");
		if(CollectionUtils.isNotEmpty(lum)){
			for(UserMention um: lum){
				save(um);
			}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see uni.edu.pe.analisisSentimental.dao.UserMentionDAO#save(uni.edu.pe.analisisSentimental.domain.UserMention)
	 */
	public void save(UserMention userMention) {
		logger.debug("<==== Inicio save ====>");	
		if(isDebugEnabled)
			logger.debug("userMention: "+ToStringBuilder.reflectionToString(userMention));
		String sql =  "INSERT INTO USER_MENTIONS (IdTweet, NombrePantallaReceptor)"+
	    "VALUES(:idTweet, :nombrePantalla)";
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("idTweet", userMention.getIdTweet());
		parameters.put("nombrePantalla", userMention.getScreenName());
		simpleJdbcTemplate.update(sql,parameters);		
	}

}
